// 请确保在相应的文件中导入所需的依赖和定义好Cloth和Wardrobe模型类
import 'package:flutter/material.dart';
import 'dart:io';
import '../../models/wardrobe.dart';
import '../../models/cloth.dart';
import './add_clothes_page.dart';

class ClothManagementPage extends StatefulWidget {
  final Wardrobe wardrobe;
  final arguments;
  

  ClothManagementPage({required this.wardrobe, this.arguments});

  @override
  _ClothManagementPageState createState() => _ClothManagementPageState();
}

class _ClothManagementPageState extends State<ClothManagementPage> {
  List<Cloth> clothes = []; // 假设你已经定义了Cloth类

  void _addcloth(Cloth result) {
    Cloth newcloth = Cloth(
        clothname: result.clothname,
        wardrobeid: '',
        clothbelong: result.clothbelong,
        clothclass: result.clothclass // 假设所有衣物都属于Clothing类别
        );
    newcloth.clothimage = result.clothimage;
    // 添加衣物的逻辑
    setState(() {
      clothes.add(newcloth);
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: Text(widget.wardrobe.wardrobename)),
      body: Column(
        children: <Widget>[
          // 添加一个列表来展示存储衣物
          Expanded(
            child: ListView.builder(
              itemCount: clothes.length,
              itemBuilder: (context, index) {
                return Card(
                  child: InkWell(
                    onTap: () {
                      showDialog(
                        context: context,
                        builder: (BuildContext context) {
                          return AlertDialog(
                            title: Text('确认删除？'),
                            content: Text('你确定要删除这件衣物吗？'),
                            actions: <Widget>[
                              TextButton(
                                child: Text('取消'),
                                onPressed: () => Navigator.of(context).pop(),
                              ),
                              TextButton(
                                child: Text('确定'),
                                onPressed: () {
                                  // 这里需要实际调用删除接口，并处理响应
                                  // 假设deleteCloth是删除衣物的异步方法
                                  // deleteCloth(_clothes[index]).then((_) {
                                  //   setState(() {
                                  //     _clothes.removeAt(index);
                                  //   });
                                  //   Navigator.of(context).pop(); // 关闭对话框
                                  // });
                                  
                                  setState(() {
                                    clothes.removeAt(index);
                                  });
                                  Navigator.of(context).pop(); // 关闭对话框
                                },
                              ),
                              
                            ],
                          );
                        },
                      );
                    },
                    child: Padding(
                      padding: EdgeInsets.all(8.0),
                      child: Column(
                        crossAxisAlignment: CrossAxisAlignment.start,
                        children: [
                          clothes[index].clothimage != null
                              ? Image.file(
                                  clothes[index].clothimage,
                                  height: 100, // 根据需要调整高度
                                  width: 100, // 根据需要调整宽度
                                )
                              : Image.asset(
                                  'assets/default_image.png',
                                  fit: BoxFit.cover,
                                  height: 100,
                                  width: 100,
                                ),
                          SizedBox(height: 8),
                          Text(clothes[index].clothname!,
                              style: TextStyle(fontSize: 18)),
                          SizedBox(height: 4),
                          Text(clothes[index].clothbelong!,
                              style:
                                  TextStyle(fontSize: 14, color: Colors.grey)),
                        ],
                      ),
                    ),
                  ),
                );
              },

              
            ),
          ),
        ],
      ),

      // 添加悬浮按钮
      floatingActionButton: FloatingActionButton(
        onPressed: () {
          Navigator.push(
            context,
            MaterialPageRoute(builder: (_) => add_cloth_page()),
          ).then((result) {
            if (result is Cloth) {
              _addcloth(result);
            };
          });
        },
        child: const Icon(Icons.add), // 使用加号图标
      ),
    );
  }
}
